<?php
require_once('popup_base_jectly.php');

/*
* sub classes of page_base_jectly will include only the changes from index_2;
*/
class popup_cancel_bid extends popup_base_jectly {

	protected $contents_html='<div class=\'center\'>This is popup_cancel_bid page.</div>';

	protected $id;//project_id
	public $project;
	public  $bid_id;//bid_id
	public $bid_cancel_comments;

	public $bid;//this can be unique only by project id but will check from query string too
	public $bool_confirming=false;
	public $bool_message_exist=false;
	public $bool_error=false;
	public $bool_permitted=false;
	public $button_confirm;
	public $button_cancel;
	public $bid_cancel;
	public $textbox_comment;
	public $str_permission_error_text;




	public $str_confirming_text='<br/><br/>Please confirm to Cancel this bid?<br /><br />';


	function __construct() {
		parent::__construct();
	}

	protected function Form_Create() {

		$this->check_query_string();

		$this->load_project();
		$this->load_bid();

		if($this->bool_cancel_bid_permitted())
		{
			$this->bool_permitted=true;


		}


		if($this->bool_permitted)
		{

			$this->button_confirm_Create();
			$this->button_cancel_Create();
			$this->textbox_comment_Create();


		}

	}


	protected function textbox_comment_Create() {
		$this->textbox_comment = new QTextBox($this);
		$this->textbox_comment->Width=400;
		$this->textbox_comment->Columns=50;
		$this->textbox_comment->Rows=10;
		$this->textbox_comment->Name = 'comment';
		//$this->textbox_comment->CssClass = 'input';
		$this->textbox_comment->Visible=true;
		$this->textbox_comment->Wrap=true;
		$this->textbox_comment->TextMode=QTextMode::MultiLine;
		$this->textbox_comment->Overflow=QOverflow::Auto;
		if($this->bool_confirming)$this->textbox_comment->Visible=false;
	}

	protected function button_confirm_Create() {
		$this->button_confirm = new QButton($this);
		$this->button_confirm->Text = 'confirm';
		$this->button_confirm->AddAction(new QClickEvent(), new QServerAction('confirm_Click'));
		$this->button_confirm->PrimaryButton = true;
	}
	protected function button_cancel_Create() {
		$this->button_cancel = new QButton($this);
		$this->button_cancel->Text = 'cancel';
		$this->button_cancel->AddAction(new QClickEvent(), new QServerAction('cancel_Click'));
		//$this->button_cancel->PrimaryButton = true;
	}
	public function confirm_Click($strFormId, $strControlId, $strParameter) {

		$objUser = unserialize($_SESSION['User']);
		$this->UserId=$objUser->Id;
		
		
		$bid_= TableProjectBid::LoadById($this->bid);
		$bid_->set_status_cancelled();
		$bid_->Enable='N';
		$bid_->Save(false,true);


		QApplication::ExecuteJavaScript('opener.location.reload(true);self.window.close();');
	}
   public function cancel_Click($strFormId, $strControlId, $strParameter) {
            QApplication::ExecuteJavaScript('self.window.close();');
        }
	protected function check_query_string()
	{
		if(is_numeric(QApplication::QueryString('id')))
		{
			$this->id=QApplication::QueryString('id');//project_id
		}

		if(is_numeric(QApplication::QueryString('bid')))
		{
			$this->bid_id=QApplication::QueryString('bid');//project_id
		}
		
	}

	protected function load_project()
	{
		if(isset($this->id) && is_numeric($this->id))
		{
			$this->project=TableProject::LoadById($this->id);

			if(isset($this->project))
			{
				$this->contents_html='project loaded-['.$this->project->Title.'] user_id=['.$this->project->UserId.']';
			}
			else
			{
				$this->contents_html='project not found id=['.$this->id.']';
				$this->id=null;
				$this->project=null;
			}
		}
	}


	protected function load_bid()
	{
		if(isset($this->id) && is_numeric($this->id) && isset($this->bid_id))
		{
			$this->bid=TableProjectBid::LoadById($this->bid_id);

			if(isset($this->bid))

			{
				$this->contents_html='project loaded-['.$this->project->Title.'] user_id=['.$this->project->UserId.'] Bid loaded-['.$this->bid->Id.']';

			}
			else
			{
				$this->contents_html='bid not found id=['.$this->bid_id.']';
				$this->id=null;
				$this->project=null;
				$this->bid_id=null;
			}
		}
	}


	//check permissions
	public function bool_project_loaded()
	{
		return isset($this->project);
	}

	public function bool_my_project()
	{
		if(isset($this->project) && isset($this->UserId))
		{
			return $this->project->bool_my_project($this->UserId);
		}

		return false;
	}

	public function bool_won_project()
	{
		if(isset($this->project) && isset($this->UserId))
		{
			return $this->project->bool_won_project($this->UserId);
		}

		return false;
	}

	protected function bool_already_bidding()
	{
		if(isset($this->project) && isset($this->UserId))
		{
			if(TableProject::bool_bidding_for_the_project($this->UserId,$this->id))
			{
				return true;
			}
		}

		return false;
	}

	protected function bool_already_won()
	{
		if(isset($this->project) && isset($this->UserId))
		{
			if(TableProject::bool_won_for_the_project($this->UserId,$this->id))
			{
				return true;
			}
		}

		return false;
	}

	public function bool_project_open()
	{
		if(isset($this->project) && isset($this->UserId))
		{
			return $this->project->bool_open_project();
		}

		return false;
	}

	protected function bool_bid_placed()
	{
		if(isset($this->project) && isset($this->UserId) && isset($this->bid))
		{

			if(TableProjectBid::bool_bid_id_placed_for_the_project($this->project->UserId,$this->project->Id,$this->bid))
			{
				return true;

			}
		}

		return false;
	}


	protected function bool_status()
	{
		if(isset($this->project)&& isset($this->UserId) && isset($this->bid))
		{
			return $this->bid->bool_active();
		}
		return false;

	}

	public function bool_project_after_picked()
	{
		if(isset($this->project) && isset($this->UserId))
		{
			return $this->project->bool_after_picked_project();
		}

		return false;
	}

	public function bool_project_bidding_end()
	{
		if(!bool_project_open() && !bool_project_after_picked())return true;

		return false;
	}
	public function reset_permission_error_text() {

		$this->str_permission_error_text='';
	}
	 public function append_line_permission_error_text($str_) {

          $this->append_permission_error_text($str_.'<br />');
        }

     public function append_permission_error_text($str_) {

            $this->str_permission_error_text.=$str_;
        }
         public function render_permission_error_text() {

            if(!$this->bool_permitted)
            {
                echo($this->str_permission_error_text);
            }
        }

	protected function bool_cancel_bid_permitted()
	{
		if(!$this->bool_logined())
		{
			  $this->reset_permission_error_text();
                $this->append_line_permission_error_text('<br/><br/> You must login to access this page at least');
			return false;
		}
		if(!$this->bool_project_loaded())    
		{
			  $this->reset_permission_error_text();
                $this->append_line_permission_error_text('<br/><br/> Project Not found');
			return false;
		}
		if($this->bool_my_project())       
		{
			  $this->reset_permission_error_text();
                $this->append_line_permission_error_text('<br/><br/> The project is yours');
			return false;
		}
		if($this->bool_already_won())       
		{
			  $this->reset_permission_error_text();
                $this->append_line_permission_error_text('<br/><br/> You are already selected for the project');
			return false;
		}
		if(!$this->bool_project_open())       
		{
			  $this->reset_permission_error_text();
                $this->append_line_permission_error_text('<br/><br/> The project is closed for bidding');
			return false;
		}
		if(!$this->bool_bid_placed())       
		{
			  $this->reset_permission_error_text();
                $this->append_line_permission_error_text('<br/><br/> You have not placed the bid');
			return false;
		}
		if(!$this->bool_status())       
		{
			$this->reset_permission_error_text();
                $this->append_line_permission_error_text('<br/><br/> You have cancelled the bid');
			return false;
		}
	

		if($this->bool_already_bidding())
		{
			return true;
		}

		return true;
	}


	public function render_contents_area() {

		if($this->bool_contents_area_on)
		{
			echo($this->contents_html);
		}
	}
	public function render_confirming_text() {

		echo($this->str_confirming_text);

	}



}

popup_cancel_bid::Run('popup_cancel_bid','popup_cancel_bid.tpl.php');

?>
